package com.sddz.pacs.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.sddz.pacs.annotation.SysLogger;
import com.sddz.pacs.dto.JsonResult;
import com.sddz.pacs.dto.PageModelParams;
import com.sddz.pacs.entity.SysUser;
import com.sddz.pacs.entity.SysUserRole;
import com.sddz.pacs.service.SysUserRoleService;
import com.sddz.pacs.service.SysUserService;
import com.sddz.pacs.support.ResultConstant;
import com.sddz.pacs.util.ShiroUtils;
import com.sddz.pacs.util.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author 时代电子
 * @since 2018-05-10
 */
@RestController
@RequestMapping("/sysUser")
@Api(description = "用户管理")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @PostMapping("/list")
    @ApiOperation(value="获取用户列表", notes="获取用户分页立标")
    @ApiImplicitParam(name = "pageModelParams",value = "分页参数json对象",required = true)
    public JsonResult list(@RequestBody PageModelParams pageModelParams){

        PageInfo<SysUser>      pageInfo = sysUserService.selectListByCondition(pageModelParams);
        return JsonResult.success(pageInfo,ResultConstant.LIST_SUCCESS);
    }

    @GetMapping("/detail/{id}")
    @SysLogger(value = "查询具体用户",type = "用户管理")
    @ApiOperation(value="查询具体用户")
    @ApiImplicitParam(name = "id",value = "用户唯一标识",required = true)
    public JsonResult findOne(@PathVariable("id") String id){
        SysUser sysUser = sysUserService.selectById(id);
        List<String> userRoleIdList = sysUserRoleService.getRoleIdList(id);
        sysUser.setRoleIdList(userRoleIdList);

        if(sysUser==null){
            return JsonResult.failure(ResultConstant.NO_SUCH_PERSON);
        }
        return JsonResult.success(sysUser,ResultConstant.SELECT_USER_SUCCESS);
    }

    @PostMapping("/add")
    @ApiOperation(value="新增系统用户")
    @ApiImplicitParam(name = "sysUser",value = "用户json对象",required = true)
    public JsonResult add(@RequestBody SysUser sysUser){

        //2.机构注册入库
        String salt = RandomStringUtils.randomAlphanumeric(20);
        String loginName=sysUser.getLoginName();
        String passWord = ShiroUtils.encodeSalt(sysUser.getPassword(),salt);
        sysUser.setSalt(salt);
        sysUser.setPassword(passWord);
        // 判读数据库中是否存在该用户
        Integer count = sysUserService.queryUserByLoginNames(loginName);
        if(count > 0){
            return JsonResult.failure(ResultConstant.USER_NAME_DUPLICATION_EXISTS);
        }
        //2.存储用户
        boolean flag = sysUserService.addUser(sysUser);

        if(!flag){
            return JsonResult.failure(ResultConstant.ADD_FAILURE);
        }
        return JsonResult.success(ResultConstant.ADD_SUCCESS);
    }

    @GetMapping("/delete/{id}")
    @SysLogger(value = "删除系统用户",type = "用户管理")
    @ApiOperation(value="删除系统用户")
    @ApiImplicitParam(name = "id",value = "用户唯一标识",required = true)
    public JsonResult delete(@PathVariable("id") String id){

        boolean flag = sysUserService.deleteById(id);

        //删除人员对应的角色
        EntityWrapper<SysUserRole> ew = new EntityWrapper<>();
        ew.eq("USER_ID",id);
        sysUserRoleService.delete(ew);

        if(!flag){
            return JsonResult.failure(ResultConstant.DELETE_FAILURE);
        }
        return JsonResult.success(ResultConstant.DELETE_SUCCESS);
    }

    @PostMapping("/update")
    @SysLogger(value = "更新系统用户",type = "用户管理")
    @ApiOperation(value="更新系统用户")
    @ApiImplicitParam(name = "sysUser",value = "用户json对象",required = true)
    public JsonResult update(@RequestBody SysUser sysUser){
        boolean flag = sysUserService.updateUser(sysUser);
        SysUser user =  sysUserService.selectById(sysUser.getId());
        if(!flag){
            return JsonResult.failure(ResultConstant.UPDATE_FAILURE);
        }
        return JsonResult.success(ResultConstant.UPDATE_SUCCESS);
    }
}
